// 资产库可用资产信息
var purchaseProductDatas = new Array();

$$scope.submmitData = {};
$$scope.submmitData.purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}
$$scope.select = {};
// 采购记录
var proc1Columns = [
    {
        id: 'commodityCateName',
        title: '类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'commodityClassName',
        title: '类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'name',
        title: '名称',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'descAndSpec',
        title: '品名及规格',
        width: 200,
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'quantity',
        title: '数量',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'estimatedAmount',
        title: '预计金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'remark',
        title: '备注',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'operation',
        title: '操作',
        width: 130,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-info" onclick="shproc(\'' + record.biCode + '\');"><i class="fa fa-edit"></i>  编辑</button>';
            content += '  ';
            if(record.quantity == 0){
                content += '<button class="btn btn-xs btn-danger" onclick="deletePurchaseSecond(\'' + record.biCode + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
            }
            return content;
        }
    }];
function isSameItemThird(element){
    return element.biCode != nowBiCode;
}
var nowBiCode;
function deletePurchaseSecond(biCode){
    nowBiCode = biCode;
    var exhibitDatas = aboveGrid.exhibitDatas;
    exhibitDatas = exhibitDatas.filter(isSameItemThird);
    aboveGrid.exhibitDatas = exhibitDatas;
    aboveGrid.refresh(false);
}

// 资产库借货记录
var library1Columns = [
//     {
//     id: 'number',
//     title: '序号',
//     width: 45,
//     type: 'string',
//     columnClass: 'text-center',
// },
 {
    id: 'asset_style2',
    title: '资产类目',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'asset_style2',
    title: '资产类别',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'asset_id',
    title: '资产编号',
    width: 170,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'comments',
    title: '资产描述',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'asset_status',
    title: '状态',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'createUserId',
    title: '归属人',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'pro_name',
    title: '归属项目',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'city_location',
    title: '放置地',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}];

function isNotSameAsset(element) {
    return element.asset_id == bigAssetCode;
}
var belowGrid;
var aboveGrid;
var brCode;
$CertusJsonAjax.ajax("purchaseBaseInfo", {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
}, function (res) {
    if (res.data) {
        $$scope.headerMap = res.data.headerMap;
        $$scope.processs = res.data.processs;
        brCode = res.data.headerMap.brCode;
        $$scope.submmitData.purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;
        $$scope.submmitData.purchaseStepEntity.projId = res.data.headerMap.projId;
        var priceOption1 = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '采购单条目信息',
            datas: res.data.buyings,
            columns: proc1Columns,
            gridContainer: 'proc3Container',
        };
        aboveGrid = $.fn.dlshouwen.grid.init(priceOption1);
        aboveGrid.load();

        var priceOption2 = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '资产库申请清单',
            datas: res.data.inventors,
            columns: library1Columns,
            gridContainer: 'library3Container'
        };
        belowGrid = $.fn.dlshouwen.grid.init(priceOption2);
        belowGrid.load();

    }
})

//初始化 页面 select
$CertusJsonAjax.ajax("purchaseProductCode", {}, function (res) {
    if (res.data) {
        var cate = res.data.cate;
        var clas = res.data.clas;

        $$scope.select.cateCodeSelect = new Array();
        for (var i in cate) {
            var split = cate[i].split(",");
            $$scope.select.cateCodeSelect.push({code: split[0], name: split[1]})
        }

        $$scope.select.classCodeSelect = new Array();
        for (var i in clas) {
            var classs = clas[i].split(",");
            ;
            $$scope.select.classCodeSelect.push({code: classs[1], name: classs[2]})
        }

    }
})

$$scope.select.bigClassSelect = [
    {name: "设备", code: "SB"},
    {name: "服务", code: "FW"},
    {name: "其他", code: "QT"}]

$$scope.sbchanged = function(a){
    if(a == "SB"){
        $$scope.sbshow = true;
        $$scope.sbbushow = false;
    }else{
        $$scope.sbshow = false;
        $$scope.sbbushow = true;
    }
}

// 显示隐藏采购表单
function shproc(biCodes) {
    $('.hdproc').hide();
    var exhibitDatas = aboveGrid.exhibitDatas;
    for(var i in exhibitDatas){
        if (exhibitDatas[i].biCode == biCodes) {
            ;
            $$scope.applyItem = exhibitDatas[i];
            $$scope.sbchanged($$scope.applyItem.bigClassCode)
        }
    }
    $$scope.bbbbb = "true";
    $$scope.$apply();
    $('.hdproc').show();
}

$$scope.applyItemSubmitThird = function(){
    if(certusValidateNotnullPolyfill()){
        var exhibitDatas = aboveGrid.exhibitDatas;
        for(var i in exhibitDatas){
            if (exhibitDatas[i].biCode == $$scope.applyItem.biCode) {
                exhibitDatas[i] = $$scope.applyItem;
            }
            aboveGrid.exhibitDatas = exhibitDatas;
            aboveGrid.refresh(false);
            $('.hdproc').hide();
        }
    }

}
//隐藏采购表单
function hdproc() {
    $('.hdproc').hide();
}

$$scope.getsssbbb = function(bb){
    ;
    //starting denied sentback others completed
    if(bb == "starting"){
        return "正在进行中";
    }else if(bb == "denied"){
        return "否决";
    }else if(bb == "sentback"){
        return "被退";
    }else if(bb == "others"){
        return "关闭";
    }else if(bb == "completed"){
         return "完成";    }else if(bb == "terminated"){        return "终结";    }    else if(bb == "cancelled"){        return "通过";}
    return "待办";
}

//提交表单
$$scope.approvalApply = function(){
    if(certusValidateNotnull()){
        $$scope.submmitData.productApplyItems = belowGrid.exhibitDatas;
        var exhibitDatas = aboveGrid.exhibitDatas;
        $$scope.submmitData.purchaseItems = exhibitDatas;
        $$scope.submmitData.findWho = "B";
        //检查 是否 预测 金额
        for(var i in exhibitDatas){
           var number = parseInt(exhibitDatas[i].estimatedAmount);
           if(number <= 0){
               layer.msg("预测金额不能为0")
               return;
           }
        }
        $CertusJsonAjax.ajax("assetManager",$$scope.submmitData,function(res){
            if(res.data){
                if("success" == res.data){
                    findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());
                    window.history.back();
                    return;
                }
                layer.msg(res.data);
            }
        })
    }

}